WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue?

When you set a list in any ItemsSource, the DataTemplate's DataContext for the individual items will each item of the list.

Up vote 0 down vote favorite share g+ share fb share tw.

My code fails at at startup because the values array in the Converter that is called by the Multibinding is not filled with proper value but have a value of DependencyProperty.UnsetValue. Have a look at Convertor and also see where I getting error public class ButtonColorConverter : IMultiValueConverter { public object Convert(object values, Type targetType, object parameter, CultureInfo culture) { string val1 = string. Format(" {0} ", values0); string val2 = (string)values1; **//Here I am getting ==> {DependencyProperty.

UnsetValue}** return val1. Equals(val2)? Brushes.

NavajoWhite : Brushes. White; } public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } You can download full code or see my codes snippet as per below. MainWindow.

Xaml **//This Binding not resolves properly** MainWindow.xaml.cs public partial class MainWindow : Window { public MainWindow() { MyPageViewModel = new PageViewModel(); MyPageViewModel.PageCollection. Add(new PageNumberViewModel(string. Format(" {0} ",0))); MyPageViewModel.PageCollection.

Add(new PageNumberViewModel(string. Format(" {0} ",1))); MyPageViewModel.PageCollection. Add(new PageNumberViewModel(string.

Format(" {0} ",2))); MyPageViewModel.PageCollection. Add(new PageNumberViewModel(string. Format(" {0} ",3))); InitializeComponent(); } public PageViewModel MyPageViewModel { get { return this.

DataContext as PageViewModel; } set { this. DataContext = value; } } } And this is ViewModel Classes. PageViewModel.cs public class PageViewModel:ViewModelBase { private ObservableCollection m_pageCollection = new ObservableCollection(); private PageNumberViewModel m_currentPage = new PageNumberViewModel(string.

Format(" {0} ",0)); public PageViewModel() { m_currentPage = new PageNumberViewModel(string. Format(" {0} ", 1000)); } public PageNumberViewModel CurrentPage { get { return this. M_currentPage; } set { if (m_currentPage == value) return; this.

M_currentPage = value; base. OnPropertyChanged("CurrentPage"); } } public ObservableCollection PageCollection { get { return this. M_pageCollection; } set { if (m_pageCollection == value) return; this.

M_pageCollection = value; base. OnPropertyChanged("PageCollection"); } } } PageNumberViewModel.cs public class PageNumberViewModel : ViewModelBase { private string m_pageNumber; public PageNumberViewModel() { } public PageNumberViewModel(string pageNumgerArg) { this. M_pageNumber = pageNumgerArg; } public string Page_Number { get { return m_pageNumber; } set { if (m_pageNumber == value) return; m_pageNumber = value; OnPropertyChanged("PageNumber"); } } } c# wpf xaml multibinding link|improve this question edited Jul 22 '11 at 7:09 asked Jul 22 '11 at 6:57Aryan SuryaWansi3135 89% accept rate.

1 Even without looking deeper into your code, I would strongly suggest to first test before you execute (string)values1 (always beware of "magic numbers") if a) values! = null and b) really contains an object at index 1 (or at least values. Length > 0) , and c) that object really has the desired type STRING.

Triggers might fire mre often and then contain different values than you might expect, so always check your Arrays before you use them that directly. That might already help you to prevent your exceptions. – Jens H Jul 22 '11 at 7:18.

When you set a list in any ItemsSource, the DataTemplate's DataContext for the individual items will each item of the list. True that your TextBlock binding is working correctly, because the DataContext is set to your main object: PageViewModel But in your DataTemplate the DataContext will be set to PageNumberViewModel, since these are the items in your collection. Therefor, the binding to Path=CurrentPage.

Page_Number will result to UnsetValue, because CurrentPage is not a property of PageNumberViewModel Hope this clarifies things! If you truly wish to bind to the CurrentPage property of your Window's DataContext, consider using an ElementName binding: Give window a name, bind to or use a RelativeSource binding.

1 Thank you so much........This is what exactly I wanted...i have just changed binding path as you have suggested..and problem being solved.....once again thank you so much...... – Aryan SuryaWansi Jul 22 '11 at 8:34 You're welcome! Glad I could help! :) – Arcturus Jul 22 '11 at 9:09.

It looks like you might not have the correct DataContext to resolve the path CurrentPage. Page_Number. A good way of debugging this sort of thing is to remove the path so that you can inspect the DataContext within your value converter: Then set a breakpoint in your ButtonColorConverter and have a look at exactly what you are trying to convert.

You can see in side my code when I bound this CurrentPage. Page_Number to TextBox's Text property it working fine. It only gives exception if we use it in Multibinding with Convertor – Aryan SuryaWansi Jul 22 '11 at 7:39 1 Yes, but isn't your content binding, Content="{Binding Path=Page_Number}", affecting the DataContext of the Button.

Background? – ColinE Jul 22 '11 at 7:58 Ya @ColinE, when I have degug as per you have suggest...i am getting {DataPager.ViewModel. PageNumberViewModel} as instead of {DataPager.ViewModel.

PageViewModel} which I have assigned as DataContext Thanks..... – Aryan SuryaWansi Jul 22 '11 at 8:26 Plus One for the debugging tip! – granadaCoder Jul 22 '11 at 20:43.

I have just Changed in MainWindow. Xaml as per Mr.Arcturus's suggestion and its working fine. Thank you so much Mr.Arcturus.

You can see ManiWindow. Xaml after chage.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions